const app = getApp();
const api = require('../../utils/api');
const md5 = require('../../utils/md5');

Page({
    data: {
        orderList: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        noContent: false, //无数据
        isFinished: false, //加载结束

        showPassword: false, //支付密码弹窗
        couponOrder: '',
        payPassword: '',
        pointAccount: 0,
    },
    onLoad(options) {
        this.getData();
    },
    onShow() {
    },

    getData() { //获取订单列表
        let params = {
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post('/coupon/order/list', params)
            .then(res => {
                let list = res.content;
                //整理数据
                list.forEach(item => {
                    item.createTime = app.filterDate(item.createTime);
                    item.payAmount = item.payAmount.toFixed(2);
                });
                let newList = [...this.data.orderList, ...list];

                this.setData({
                    orderList: newList,
                    total: res.total
                });

                //是否无内容标识
                if(this.data.orderList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.orderList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    })
                } else {
                    this.setData({
                        isFinished: false
                    })
                }
                wx.stopPullDownRefresh();
            })
    },
    getPointAccount() { //获取积分账户余额
        api.post(`/user/member/account`)
            .then(res => {
                this.setData({
                    pointAccount: res.balance
                });
            })
    },

    //订单操作
    viewDetail(e) { //查看详情
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/couponOrderDetail/couponOrderDetail?id=' + id,
        });
    },
    gotoPay(e) { //去支付
        let item = e.currentTarget.dataset.item;
        let type = e.currentTarget.dataset.type;
        if(type == 1) { //积分支付
            this.getPointAccount();
            this.setData({
                couponOrder: item,
                showPassword: true,
            });
        } else {
            wx.navigateTo({
                url: `/pages/orderPay/orderPay?id=${item.id}&type=1`,
            });
        }
    },
    cancelOrder(e) { //取消订单
        let item = e.currentTarget.dataset.item;
        let index = e.currentTarget.dataset.index;
        wx.showModal({
            title: '温馨提示',
            content: `您是否确认取消购买【${item.productName}】？`,
            success: res => {
                if(res.confirm) {
                    api.post('/coupon/order/cancel?orderId=' + item.id)
                        .then(() => {
                            wx.showToast({
                                title: '订单已取消',
                                icon: 'none'
                            });

                            //刷新列表数据
                            this.setData({
                                [`orderList[${index}].orderStatus`]: 3
                            });
                        })
                }
            }
        });
    },

    //支付密码弹窗
    bindinput(e) { //输入框监听
        let item = e.currentTarget.dataset.item;
        let val = e.detail.value;
        this.setData({
            [item]: val
        });
    },
    submitPassword() { //提交支付密码
        if(!this.data.payPassword) {
            wx.showToast({
                title: `请输入支付密码`,
                icon: 'none'
            });
            return;
        }
        
        let password = this.data.payPassword;
        api.post('/payment/credits/pay', {
            orderId: this.data.couponOrder.id,
            abc: md5.md5(password)
        })
            .then(() => {
                wx.showToast({
                    title: '支付成功',
                    icon: 'success'
                });

                //刷新列表数据
                let index = this.data.orderList.findIndex(item => {
                    return item.id == this.data.couponOrder.id;
                });
                this.setData({
                    [`orderList[${index}].orderStatus`]: 2
                });

                this.closePassword();
            })
    },
    closePassword() { //关闭支付密码弹窗
        this.setData({
            payPassword: '',
            showPassword: false,
        });
    },
    
    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            orderList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.getData();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getData();
            }, 800);
        }
    },
})